package com.song.EMS.dao.impl;

import com.song.EMS.bean.User;
import com.song.EMS.dao.UserDao;
import com.song.EMS.utils.DruidUtil;
import com.song.EMS.utils.TransformUtil;

import javax.management.relation.Role;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends DruidUtil implements UserDao {
    /**通过手机号修改登录时间的sql*/
    private static final String UPDATE_LOGIN_TIME_SQL = "update user set loginTime = ? where userPhone = ?";

    /**查询控制台数据的sql*/
    private static final String CONSOLE = "select count(userId) total,count(to_days(registerTime)=to_days(now()) or null) day from user";

    /**用户的新增sql*/
    private static final String INSERT_SQL = "INSERT INTO USER VALUES(NULL,?,?,?,?,?,null,?)";
    /**
     * 通过手机号或用户名查询的sql
     */
    private static final String SELECTBYPHONE_SQL = "SELECT * FROM USER WHERE";
    /**
     * 查询用户所有信息sql
     */
    private static final String SELECTALL_SQL = "SELECT * FROM USER";
    /**
     * 修改用户信息sql
     */
    private static final String UPDATE_SQL = "UPDATE USER SET userName = ?,IDCard = ?, userGender = ?,password = ?,userPhone = ? WHERE userId = ?";
    /**
     * 删除用户sql
     */
    private static final String DELETE_SQL = "DELETE FROM USER WHERE userId = ?";


    /**
     * 获取用户控制台数据
     *
     * @return 数据集合
     */
    @Override
    public List userConsole() {
        List list = new ArrayList<>();
        try {
            /*resultSet = select(COUNTALL_SQL, null);
            while (resultSet.next()) {
                list.add(resultSet.getInt("total"));
            }

            resultSet = select(CONUT_SQL, null);
            while(resultSet.next()){
                list.add(resultSet.getInt("day"));
            }*/

            resultSet = select(CONSOLE, null);
            while (resultSet.next()) {
                list.add(resultSet.getInt("total"));
                list.add(resultSet.getInt("day"));
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return null;
    }

    /**
     * 查询所有用户信息
     *
     * @param limit     是否分页查询 true 分页   false  不分页
     * @param pageIndex sql语句查询的起始索引
     * @param pageSize  一次查询的数量
     * @return 所有用户信息集合
     */
    @Override
    public List<User> selectUsers(Boolean limit, Integer pageIndex, Integer pageSize) {
        String sql = null;
        List list = new ArrayList<>();
        List<User> users = new ArrayList<>();
        if (limit) {
            sql = SELECTALL_SQL + " limit ?,?";
            list.add(pageIndex);
            list.add(pageSize);
        } else {
            sql = SELECTALL_SQL;
        }
        try {
            resultSet = select(sql, list);
            while (resultSet.next()) {
                User user = new User();
                user.setUserName(resultSet.getString("userName"));
                user.setIDCard(resultSet.getString("IDCard"));
                user.setUserGender(resultSet.getInt("userGender"));
                user.setPassword(resultSet.getString("password"));
                user.setUserPhone(resultSet.getString("userPhone"));
                user.setLoginTime(resultSet.getTimestamp("loginTime"));
                user.setRegisterTime(resultSet.getTimestamp("registerTime"));
                user.setUserId(resultSet.getInt("userId"));
                users.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return users;
    }

    /**
     * 通过手机号或用户名查询用户
     *
     * @param userPhone 用户的手机号
     * @return 查询的用户对象
     */
    @Override
    public User selectByPhone(String userPhone, String userName) {
        User user = new User();
        String sql = null;
        try {
            List list = new ArrayList<>();

            if (userName != null && !"".equals(userName)) {
                sql = SELECTBYPHONE_SQL + " userName = ?";
                list.add(userName);
            } else if (userPhone != null && !"".equals(userPhone)) {
                sql = SELECTBYPHONE_SQL + " userPhone = ?";
                list.add(userPhone);
            }
            resultSet = select(sql, list);
            while (resultSet.next()) {
                user.setUserName(resultSet.getString("userName"));
                user.setIDCard(resultSet.getString("IDCard"));
                user.setUserGender(resultSet.getInt("userGender"));
                user.setPassword(resultSet.getString("password"));
                user.setUserPhone(resultSet.getString("userPhone"));
                user.setUserId(resultSet.getInt("userId"));
                return user;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return null;

    }

    /**
     * 用户信息的修改更新
     *
     * @param user 修改后的用户信息
     * @return 1成功  0用户名已被用  -1手机号码已被注册  -2失败
     */
    @Override
    public Integer updateUser(User user) {

        List list = new ArrayList<>();
        list.add(user.getUserName());
        list.add(user.getIDCard());
        list.add(user.getUserGender());
        list.add(user.getPassword());
        list.add(user.getUserPhone());
        list.add(user.getUserId());
        try {
            int update = update(UPDATE_SQL, list);
            if (update > 0) {
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return -2;
    }

    /**
     * 通过用户ID删除用户
     *
     * @param userId 用户的ID
     * @return true 删除成功  false 删除失败
     */
    @Override
    public Boolean deleteUser(Integer userId) {
        List list = new ArrayList<>();
        list.add(userId);
        try {
            int update = update(DELETE_SQL, list);
            if (update > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return false;
    }

    /**
     * 通过快递员手机号修改其登录记录
     *
     * @param userPhone 用户手机号
     */
    @Override
    public void updateLoginTime(String userPhone) {
        try {
            List<Object> list = new ArrayList<>();
            list.add(TransformUtil.getTimestamp());
            list.add(userPhone);
            update(UPDATE_LOGIN_TIME_SQL,list);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
    }

    /**
     * 用户的新增
     *
     * @param user 用户注册信息
     * @return 1成功 2用户名已被注册  3手机号已被注册 4失败
     */
    @Override
    public Integer insertUser(User user) {
        List list = new ArrayList<>();

        list.add(user.getUserName());
        list.add(user.getUserGender());
        list.add(user.getIDCard());
        list.add(user.getPassword());
        list.add(user.getUserPhone());

        list.add(TransformUtil.getTimestamp());
        try {

            if (update(INSERT_SQL, list) > 0) {
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }

        return -2;

    }


}
